{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy import genfromtxt\n",
    "from sklearn import linear_model\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[      nan       nan       nan       nan       nan       nan       nan\n",
      "        nan]\n",
      " [      nan    83.      234.289   235.6     159.      107.608  1947.\n",
      "     60.323]\n",
      " [      nan    88.5     259.426   232.5     145.6     108.632  1948.\n",
      "     61.122]\n",
      " [      nan    88.2     258.054   368.2     161.6     109.773  1949.\n",
      "     60.171]\n",
      " [      nan    89.5     284.599   335.1     165.      110.929  1950.\n",
      "     61.187]\n",
      " [      nan    96.2     328.975   209.9     309.9     112.075  1951.\n",
      "     63.221]\n",
      " [      nan    98.1     346.999   193.2     359.4     113.27   1952.\n",
      "     63.639]\n",
      " [      nan    99.      365.385   187.      354.7     115.094  1953.\n",
      "     64.989]\n",
      " [      nan   100.      363.112   357.8     335.      116.219  1954.\n",
      "     63.761]\n",
      " [      nan   101.2     397.469   290.4     304.8     117.388  1955.\n",
      "     66.019]\n",
      " [      nan   104.6     419.18    282.2     285.7     118.734  1956.\n",
      "     67.857]\n",
      " [      nan   108.4     442.769   293.6     279.8     120.445  1957.\n",
      "     68.169]\n",
      " [      nan   110.8     444.546   468.1     263.7     121.95   1958.\n",
      "     66.513]\n",
      " [      nan   112.6     482.704   381.3     255.2     123.366  1959.\n",
      "     68.655]\n",
      " [      nan   114.2     502.601   393.1     251.4     125.368  1960.\n",
      "     69.564]\n",
      " [      nan   115.7     518.173   480.6     257.2     127.852  1961.\n",
      "     69.331]\n",
      " [      nan   116.9     554.894   400.7     282.7     130.081  1962.\n",
      "     70.551]]\n"
     ]
    }
   ],
   "source": [
    "data = np.genfromtxt(r'longley.csv',delimiter=',')\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  234.289   235.6     159.      107.608  1947.       60.323]\n",
      " [  259.426   232.5     145.6     108.632  1948.       61.122]\n",
      " [  258.054   368.2     161.6     109.773  1949.       60.171]\n",
      " [  284.599   335.1     165.      110.929  1950.       61.187]\n",
      " [  328.975   209.9     309.9     112.075  1951.       63.221]\n",
      " [  346.999   193.2     359.4     113.27   1952.       63.639]\n",
      " [  365.385   187.      354.7     115.094  1953.       64.989]\n",
      " [  363.112   357.8     335.      116.219  1954.       63.761]\n",
      " [  397.469   290.4     304.8     117.388  1955.       66.019]\n",
      " [  419.18    282.2     285.7     118.734  1956.       67.857]\n",
      " [  442.769   293.6     279.8     120.445  1957.       68.169]\n",
      " [  444.546   468.1     263.7     121.95   1958.       66.513]\n",
      " [  482.704   381.3     255.2     123.366  1959.       68.655]\n",
      " [  502.601   393.1     251.4     125.368  1960.       69.564]\n",
      " [  518.173   480.6     257.2     127.852  1961.       69.331]\n",
      " [  554.894   400.7     282.7     130.081  1962.       70.551]]\n",
      "[  83.    88.5   88.2   89.5   96.2   98.1   99.   100.   101.2  104.6\n",
      "  108.4  110.8  112.6  114.2  115.7  116.9]\n"
     ]
    }
   ],
   "source": [
    "x_data  =data[1:,2:]\n",
    "y_data = data[1:,1]\n",
    "print(x_data)\n",
    "print(y_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42.9649800509\n",
      "[ 0.1016487   0.00416716  0.00349843  0.          0.          0.        ]\n"
     ]
    }
   ],
   "source": [
    "model = linear_model.ElasticNetCV()\n",
    "model.fit(x_data,y_data)\n",
    "\n",
    "print(model.alpha_)  #弹性往系数\n",
    "print(model.coef_)  #相关系数 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 115.6037171])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#做预测\n",
    "model.predict(x_data[-2,np.newaxis])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
